package com.agile.springboot.controller;

import com.agile.springboot.common.Result;
import com.agile.springboot.entity.Group;
import com.agile.springboot.entity.User;
import com.agile.springboot.service.GroupService;
import com.agile.springboot.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/group")
public class GroupController {

    @Resource
    UserService userService;
    @Resource
    GroupService groupService;

    /**
     * 新增群组
     * @param group 信息尽可能完整
     * @return 结果相关信息
     */
    @PostMapping("/addGroup")
    public Result<?> addGroup(@RequestBody Group group){
        if(groupService.addGroup(group)==1){
            return Result.success(group, "添加群组成功");
        }
        return Result.error("-1","添加群组失败");
    }

    /**
     * 逻辑删除群组
     * @param group 群组实体
     * 所必须的参数为 id 群组id
     * @return 结果相关信息
     */
    @PostMapping("/deleteGroup")
    public Result<?> deleteGroup(@RequestBody Group group){
        if(groupService.delGroup(group.getId())==1){
            return Result.success(group, "删除群组成功");
        }
        return Result.error("-1","删除群组失败");
    }

    /**
     * 物理删除群组
     * @param group 群组实体
     * 所必须的参数为 id 群组id
     * @return 结果相关信息
     */
    @PostMapping("/pscDeleteGroup")
    public Result<?> pscDeleteGroup(@RequestBody Group group){
        if(groupService.pscDelGroup(group.getId())==1){
            return Result.success(group, "删除群组成功");
        }
        return Result.error("-1","删除群组失败");
    }

    /**
     * 修改群组信息
     * @param group 群组实体
     * 所必须的参数为 id 群组id
     * @return 结果相关信息
     */
    @PostMapping("/updateGroup")
    public Result<?> updateGroup(@RequestBody Group group){
        if(groupService.updateGroup(group)==1){
            return Result.success(group, "修改群组信息成功");
        }
        return Result.error("-1","修改群组信息失败");
    }

    /**
     * 模糊搜索群组
     * @param group 群组实体
     * 所必须的参数为 group_name 群组名
     * @return List<Group> 群组列表
     */
    @PostMapping("/fuzzySearchGroupByName")
    public Result<?> fuzzySearchGroupByName(@RequestBody Group group){
        List<Group> groups = groupService.fuzzySearchGroupByName(group.getGroup_name());
        if(!groups.isEmpty()){
            return Result.success(groups, "查找群组成功");
        }
        return Result.error("-1","无相关群组！");
    }

    /**
     * 精准搜索群组——根据群组名
     * @param group 群组实体
     * 所必须的参数为 group_name 群组名
     * @return Group实体
     */
    @PostMapping("/exactSearchGroupByName")
    public Result<?> exactSearchGroupByName(@RequestBody Group group){
        Group group0 = groupService.exactSearchGroupByName(group.getGroup_name());
        if(group0!=null){
            return Result.success(group0, "查找群组成功");
        }
        return Result.error("-1","无相关群组！");
    }

    /**
     * 精准搜索群组——根据群组id
     * @param group 群组实体
     * 所必须的参数为 id 群组id
     * @return Group实体
     */
    @PostMapping("/searchGroupById")
    public Result<?> searchGroupById(@RequestBody Group group){
        Group group0 = groupService.searchGroupById(group.getId());
        if(group0!=null){
            return Result.success(group0, "查找群组成功");
        }
        return Result.error("-1","无相关群组！");
    }

    /**
     * 查询当前用户创建的所有群组——根据当前用户id
     * @param user 用户实体
     * 所必须的参数为 id 用户的id
     * @return 结果相关信息和 List<Group> 用户创建的所有群组
     */
    @PostMapping("/queryGroupsCreatedByUser")
    public Result<?> queryGroupsCreatedByUser(@RequestBody User user){
        User the_user = userService.searchUserById(user.getId());
        if(the_user==null){
            return Result.error("0","不存在此用户id!");
        }
        if(the_user.getIdentity()==2){//辨别用户身份是否为用户
            List<Group> groupList = groupService.queryGroupsCreatedByUser(the_user.getId());//查询当前用户创建的群组
            if(groupList.size()!=0){
                return Result.success(groupList,"查询成功！");
            }else{
                return Result.error("-1","该用户没有创建过群组！");
            }
        }else {
            return Result.error("-2","此id所对应的不是用户！");
        }
    }

    /**
     * 查询当前答者所属的所有群组——根据当前用户id
     * @param user 答者实体
     * 所必须的参数为 id 答者的id
     * @return 结果相关信息和 List<Group> 答者所属的所有群组
     */
    @PostMapping("/queryGroupsBelongToAnswerer")
    public Result<?> queryGroupsBelongToAnswerer(@RequestBody User user){
        User the_user = userService.searchUserById(user.getId());
        if(the_user==null){
            return Result.error("0","不存在此用户id!");
        }
        if(the_user.getIdentity()==3){//辨别用户身份是否为用户或答者
            List<Group> groupList = groupService.queryGroupsBelongToAnswerer(the_user.getId());//查询当前用户或答者所属的群组
            if(groupList.size()!=0){
                return Result.success(groupList,"查询成功！");
            }else{
                return Result.error("-2","该答者不属于任何群组！");
            }
        }else {
            return Result.error("-3","此id所对应的不是答者！");
        }
    }

    /**
     * 向群组中添加答者
     * @param dataMap 群组id和需要向该群组中添加的答者id列表
     * 所必须的参数为 group_id 群组id和 member_id 列表
     * @return 成功添加的答者数量
     */
    @PostMapping("/addAnswererToGroup")
    public Result<?> addAnswererToGroup(@RequestBody Map<String,List<String>> dataMap){
        String group_id = dataMap.get("group_id").get(0);
        List<String> memberIdList = dataMap.get("member_id");
        int num = 0;
        for(String memberId : memberIdList){
            if(groupService.addAnswererToGroup(group_id,memberId)==1){
                num++;
            }
        }
        if(num==0){
            return Result.error("-1","当前群组中已经存在了这批答者！");
        }else{
            Group group = groupService.searchGroupById(group_id);//获取当前群组信息
            group.setTotal_number((group.getTotal_number() == null ? 0 : group.getTotal_number()) + num);//群组中答者数量需要加上 num
            groupService.updateGroup(group);//更新群组中答者数量
            return Result.success(num,"添加答者成功!");
        }
    }

    /**
     * 删除群组中的一些答者
     * @param dataMap 群组id和该群组下需要删除的答者id列表
     * 所必须的参数为 group_id 群组id和 member_id列表
     * @return 成功删除的答者数量
     */
    @PostMapping("/deleteAnswerersFromGroup")
    public Result<?> deleteAnswerersFromGroup(@RequestBody Map<String,List<String>> dataMap ){
        String group_id = dataMap.get("group_id").get(0);
        List<String> memberIdList = dataMap.get("member_id");
        int num = 0;
        for(String memberId : memberIdList){
            if(groupService.deleteAnswerersFromGroup(group_id,memberId)==1){
                num++;
            }
        }
        if(num==0){
            return Result.error("-1","该群组下已不存在这些答者！");
        }else{
            Group group = groupService.searchGroupById(group_id);//获取当前群组信息
            group.setTotal_number((group.getTotal_number() == null ? 0 : group.getTotal_number()) - num);//群组中答者数量需要减去 num
            groupService.updateGroup(group);//更新群组中答者数量
            return Result.success(num,"删除成功！");
        }
    }

    /**
     * 查询出该群组的所有答者信息 同时返回用户加入该群组的时间——根据群组id
     * @param group
     * 所必须的参数为群组group的id
     * @return List<Map<String,String>> List中每一个map都对应一个答者，map中存储的信息是答者信息
     */
    @PostMapping("/getUsersInGroup")
    public Result<?> getUsersInGroup(@RequestBody Group group){
        if(groupService.searchGroupById(group.getId())==null){
            return Result.error("0","该群组不存在！");
        }else
        {
            List<String> idList = groupService.getUsersIdInGroup(group.getId());//答者id列表
            List<Map<String,String>> answerList = new ArrayList<>();//存储答者列表
            for(String id : idList){
                User user = userService.searchUserById(id);//获取当前答者信息
                Date date = groupService.getUserJoinTime(group.getId(),id);//获取当前答者加入该群组的时间
                //将答者相关信息存入其中
                Map<String,String> answer = new HashMap<>();//存储某一个答者的各项信息
                answer.put("id",user.getId());
                answer.put("username",user.getUsername());
                answer.put("password",user.getPassword());
                answer.put("phone",user.getPhone());
                answer.put("age",user.getAge()+"");
                answer.put("profession",user.getProfession());
                answer.put("institution",user.getInstitution());
                answer.put("identity",user.getIdentity()+"");
                answer.put("status",user.getStatus());
                answer.put("created_by",user.getCreated_by());
                answer.put("creation_date",user.getCreation_date()+"");
                answer.put("last_updated_by",user.getLast_updated_by());
                answer.put("last_update_date",user.getLast_update_date()+"");
                answer.put("is_deleted",user.getIs_deleted()+"");
                answer.put("parent_id",user.getParent_id());
                answer.put("questionnaire_count",user.getQuestionnaire_count());
                answer.put("join_time",date+"");
                answerList.add(answer);
            }
            if(answerList.size()==0){
                return Result.error("-1","该群组下不存在答者！");
            }else {
                return Result.success(answerList, "查询成功！");
            }
        }
    }

    /**
     * 查询不在该群组中的所有答者信息 同时返回用户加入该群组的时间——根据群组id
     * @param group
     * 所必须的参数为 id 群组id
     * @return List<User> 答者的列表
     */
    @PostMapping("/getUsersNotInGroup")
    public Result<?> getUsersNotInGroup(@RequestBody Group group){
        if(groupService.searchGroupById(group.getId())==null){
            return Result.error("0","该群组不存在！");
        }else{
            List<User> memberList = groupService.getUsersIdNotInGroup(group.getId());
            if(memberList.size()==0){
                return Result.error("-1","不存在该群组之外的答者！");
            }else {
                return Result.success(memberList, "查询成功！");
            }
        }
    }
}
